
$(function(){
	$("select").each(function(){
		$(this).append("<option value=''>---请选择---</option>");
	});

	var basePath = $("#basePath").val();
	$.ajax({
		url: basePath + "base_info/ajax_un_pigeonhole_base_data",
		type: "GET",
		dataType: "json",
		success: function( datas ) {
			console.log(datas);
			init(datas);
			
		}
	});
	
	
	$("input[name$='qualityControlDesc']").autocomplete({
		source: function( request, response ) {
			var eventObj = $(this.element);
			$.ajax({
				url: basePath + "base_info/ajax_quality_control_by_like_project",
				dataType: "json",
				data:{
					queryName: request.term
				},
				success: function( data ) {
				response( $.map( data, function( item ) {
					return {
						label:item.label,
						uniq_code:item.uniq_code
					}
				}));
			}
		});
		},
		minLength: 2,
		select: function( event, ui ) {
		},search: function( event, ui ) {
		}
	});
});

function init(baseInfo){
	var medicalDepts = baseInfo['medicalDepts'];
	$("select[name='outDeptCode']").each(function(){
		var options = "";
		for(var index in medicalDepts){
			var medicalDept = medicalDepts[index];
	    	options+="<option value='"+medicalDept.uniq_code+"'>"+medicalDept.label+"</option>"
	    } 
       $(this).append(options);
       
	});
}


/*点击查询按钮*/
function queryBtnClick(){
	queryFormSubmit();
}

/*提交查询*/
function queryFormSubmit(){
	var submitData = $('#queryForm').serialize();
	var basePath = $("#basePath").val();
	layer.load(1);
	$("#export_btn").attr("hidden",true);
	clearPage();
	$.ajax({
		url: basePath + "medical_record_quality_control/ajax_query_quality_control_imperfection",
		dataType: "json",
		data:submitData,
		success: function( data ) {
			var success = data['success'];
			if(success){
				
				var params = data['data'];
				var totalCounts = params['totalCounts'];
				if(totalCounts > 0){
					layer.msg("查询成功");
					var medicalRecords = params['queryDatas'];
					addRows(medicalRecords);
					$("#export_btn").removeAttr("hidden");
					iframeAutoHeight("mainContent");
				}else{
					layer.msg("查询结果为空");
				}
				
			}else{
				layer.msg("查询失败");
			}
			console.log(data);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			layer.msg("未知错误，请联系管理员");
		},
		complete:function(XMLHttpRequest, textStatus){
			layer.closeAll('loading');
			parent.document.getElementById("mainContent").height=document.body.scrollHeight + 50;
		}
	});
	
};

/*添加列表行*/
function addRows(medicalRecords){
	var basePath = $("#basePath").val();
	for(var index in medicalRecords){
		var medicalRecord = medicalRecords[index];
		var add_content=$("#template_tr").clone();
		add_content.removeAttr("hidden");
		add_content.attr("id",medicalRecord.visit_guid);
		add_content.find("span[class='only_id']").html(medicalRecord.only_id);
		add_content.find("span[class='mr_id']").html(medicalRecord.mr_id);
		add_content.find("span[class='patient_name']").html(medicalRecord.patient_name);
		add_content.find("span[class='in_hospital_date']").html(medicalRecord.in_hospital_date_format);
		add_content.find("span[class='out_dept_name']").html(medicalRecord.out_dept_name);
		add_content.find("span[class='out_hospital_date']").html(medicalRecord.out_hospital_date_format);
		add_content.find("span[class='second_level_name']").html(medicalRecord.second_level_name);
		add_content.find("span[class='third_level_name']").attr("title",medicalRecord.third_level_name).html(medicalRecord.third_level_name);
		add_content.find("span[class='remark']").attr("title",medicalRecord.remark).html(medicalRecord.remark);
		add_content.find("span[class='deduction']").html(medicalRecord.deduction);
		$("#query_show_table tbody").append(add_content);
    } 
	
}

/*清空列表*/
function clearPage(){
	$("#template_tr").siblings("tr").remove();
}



function exportToExcel(){
	var basePath = $("#basePath").val();
	$("#queryForm").attr("action",basePath+"medical_record_quality_control/imperfection_to_excel");
	$("#queryForm").submit();
}
